package com.jxc.jxc01.hch.web;

import com.alibaba.fastjson2.JSONObject;
import com.jxc.jxc01.hch.entity.Commodity;
import com.jxc.jxc01.hch.entity.Inventory;
import com.jxc.jxc01.hch.entity.PageHelper;
import com.jxc.jxc01.hch.entity.RespResult;
import com.jxc.jxc01.hch.service.IInventoryService;
import com.jxc.jxc01.hch.service.Impl.IInventoryServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("*.inventory")
public class InventoryServlet extends HttpServlet {
    IInventoryService inventoryService = new IInventoryServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String uri = req.getRequestURI();
        System.out.println(uri);
        switch (uri) {
            case "/jxc01/list.inventory":
                list(req, resp);
                break;
            case "/jxc01/query.inventory":
                query(req, resp);
                break;
            case "/jxc01/increase.inventory":
                increase(req, resp);
                break;
            case "/jxc01/modify.inventory":
                modify(req, resp);
                break;
            case "/jxc01/delete.inventory":
                delete(req, resp);
                break;
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            int id = Integer.parseInt(req.getParameter("id"));
            inventoryService.delete(id);
            resp.sendRedirect("kucunliebiao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageHelper<Inventory> pageHelper = new PageHelper<>();
            PrintWriter out = resp.getWriter();
            //获取页码页容
            int pageNumber = Integer.parseInt(req.getParameter("pagenumber"));
            int pageSize = Integer.parseInt(req.getParameter("pagesize"));
            pageHelper.setPageNum(pageNumber);
            pageHelper.setPageSize(pageSize);
            HttpSession session = req.getSession();
            if (session.getAttribute("query")==null) {
                pageHelper = inventoryService.list(pageHelper);
            }else {
                pageHelper= (PageHelper<Inventory>) session.getAttribute("query");
            }
            RespResult respResult = new RespResult(0,"ok",pageHelper.getTotal(),pageHelper.getData());
            out.print(JSONObject.toJSONString(respResult));
            session.removeAttribute("query");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void modify(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
            Inventory inventory = new Inventory();
            inventory.setId(Integer.parseInt(req.getParameter("id")));
            inventory.setName(req.getParameter("name"));
            inventory.setCategory(req.getParameter("category"));
            inventory.setInventorynumber(Integer.parseInt(req.getParameter("inventorynumber")));
            inventory.setUnit(req.getParameter("unit"));
            inventory.setWarehousename(req.getParameter("warehousename"));
            inventory.setRemarks(req.getParameter("remarks"));
            inventoryService.modify(inventory);
            resp.sendRedirect("kucunliebiao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void increase(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
            Inventory inventory = new Inventory();
            inventory.setName(req.getParameter("name"));

            System.out.println(req.getParameter("name"));
            System.out.println(req.getParameter("category"));

            inventory.setCategory(req.getParameter("category"));
            inventory.setInventorynumber(Integer.parseInt(req.getParameter("inventorynumber")));
            inventory.setUnit(req.getParameter("unit"));
            inventory.setWarehousename(req.getParameter("warehousename"));
            inventory.setRemarks(req.getParameter("remarks"));
            inventoryService.increase(inventory);
            resp.sendRedirect("kucunliebiao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void query(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageHelper<Inventory> pageHelper = new PageHelper<>();
            PrintWriter out = resp.getWriter();
            //获取页码页容
//            int pageNumber = Integer.parseInt(req.getParameter("pagenumber"));
//            int pageSize = Integer.parseInt(req.getParameter("pagesize"));
            int pageSize=10;
            int pageNumber=1;
            String name = req.getParameter("name");
            pageHelper.setPageNum(pageNumber);
            pageHelper.setPageSize(pageSize);
            pageHelper = inventoryService.query(pageHelper,name);
            HttpSession session = req.getSession();
            session.setAttribute("query",pageHelper);
            RespResult respResult = new RespResult(0,"ok",pageHelper.getTotal(),pageHelper.getData());
            out.print(JSONObject.toJSONString(respResult));
            resp.sendRedirect("kucunliebiao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}